# core.py: contains core functionality
#from google.appengine.ext import webapp # orig
#from google.appengine.ext.webapp import util # orig
from google.appengine.ext import db
import logging


class LARPContact (db.Model):               # Used for people and venues. Note: all optional!
    name = db.StringProperty()              # The responsible person for the event (or site, later)
    title = db.StringProperty()             # The purpose of this contact. e.g. "Event Organiser", "Venue Details"
    address = db.PostalAddressProperty()    # The postal address of the site, not including postcode
    postcode = db.StringProperty()          # The postcode of the site, DH11PZ
    phone = db.PhoneNumberProperty()        # The contact phone number.
    email = db.EmailProperty()
    geopt = db.GeoPtProperty()              # Latitude Longitude. Expects floats.
    url=db.StringProperty()                 # Nice lookup
    owner = db.UserProperty()               # The *user* controlling this event.

    prettyindex = {
    'name':'Name',
    'title':'Title',
    'address':'Address',
    'postcode':'Postcode',
    'phone':'Phone Number',
    'email':'Email Address',
    'geopt':'Latitude/Longitude',
    'url':'URL'}
    
class LARPEvent(db.Expando):
    name = db.StringProperty(required=True) # The name of the entry.
    date = db.DateProperty()                # The date of the event.
    owner = db.UserProperty()               # The *user* controlling this event.
    rand = db.IntegerProperty()             # Gibberish
    created = db.DateTimeProperty(auto_now_add=True) # the creation time of the record
    url = db.StringProperty()               # The nice url [e.g. 'Maelstrom_E4_2010' -> /event/Maelstrom_E4_2010.]
    tagline = db.StringProperty()           # The tagline of the LARP
    description = db.StringProperty()       # The long description of the LARP # TODO: allow multiline
    starttime=db.DateTimeProperty()         # When is the event?
    endtime=db.DateTimeProperty()
    contacts = db.ListProperty(str)         
   
    prettyindex={
      'name':'Name',
      'date':'When?',
      'owner':'Owned by',
      'rand':'Random Number',
      'created':'Record created',
      'url':'URL',
      'tagline':'Tagline',
      'description':'Description of event',
      'contacts':'Associated People and Places',
      'starttime':'Start',
      'endtime':'Finish'}

def parseattr(attr,value):
    # so, we've been passed an attribute, probably something like event.jam: we now need to find out what sort of thing
    # jam is supposed to be in the model and provide it in the correct form. [This probably mostly applies to numbers
    # and dates.] Assume attr to be a string for now.
    # TODO: make this do more than just check the type of a string; make this actually return ints when ints are needed etc.
    modeltype=type(getattr(LARPEvent,attr)) # type of type() is 'type'.
    # consider using .rpartition('.')[2] on a string
    logging.info("%s is of type %s" % (attr,modeltype))
    logging.info("%s is a date/time: %s" % (attr, type(db.DateTimeProperty())==modeltype)) 
    # which is more efficient: using string ops or creating a new db item and then destroying it several times?


    
    
     
    
